﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Yoozoo.Modules.InnerCity;
[ExecuteInEditMode]
public class GridSpawner : MonoBehaviour
{

    [HideInInspector]
    public float CellSizeX = 0.015625f;
    [HideInInspector]
    public float CellSizeZ = 0.015625f;
    
    public int X = 9;
    public int Z = 9;


    private GameObject gridObj;
    private Mesh gridMesh;
#if UNITY_EDITOR
      public void SpawnGrid()
    {
        if (gridObj !=null)
        {
            GameObject.DestroyImmediate(gridObj);
        }
        
        GameObject gridPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/ResourcesAssets/City/Prefabs/BuildingGrid.prefab");
        gridObj = GameObject.Instantiate(gridPrefab);
        gridObj.AddComponent<DebugGridFlag>();
        
        gridMesh = gridObj.GetComponent<MeshFilter>().sharedMesh;
        
        List<Vector3> vertices = new List<Vector3>();
        List<Vector2> uvs = new List<Vector2>();
        List<int> triangles = new List<int>(6);

        for (int i = 0; i <= 1; i++)
        {
            for (int j = 0; j <= 1; j++)
            {
                vertices.Add(new Vector3(i * CellSizeX * X, j * CellSizeZ * Z));
                uvs.Add(new Vector2(i*X,j*Z));
            }
        }
        
        triangles.Add(0);
        triangles.Add(2);
        triangles.Add(1);
        
        triangles.Add(1);
        triangles.Add(2);
        triangles.Add(3);
        
        gridMesh.SetVertices(vertices);
        gridMesh.SetUVs(0,uvs);
        gridMesh.SetTriangles(triangles,0);

        var p1 = MainPos2ViewPos(Vector3.zero);
        
        var p2 = new Vector3(
            p1.x-CellSizeX * X * 0.5f ,
            0f,
            p1.y-CellSizeZ * Z * 0.5f);
        gridObj.transform.position = ViewPos2MainPos(p2);
        gridObj.transform.eulerAngles = new Vector3(-90,0,-45);

        transform.position = Vector3.zero;

        //Vector3 cacheScale = transform.localScale;
        
        transform.localScale = new Vector3(0.15f,0.15f,0.15f);


        gridObj.transform.parent = this.transform;
        gridObj.transform.localPosition = new Vector3(
            gridObj.transform.localPosition.x,
            //0.01f,
            0.066666f,        // TODO 牵扯到主城到物体的坐标转换，暂时写死
            gridObj.transform.localPosition.z);
        gridObj.transform.name = "GTA_GRID_DEBUG_OBJ";


        //transform.localScale = cacheScale;
    }

#endif
  

    public void DestroyGird()
    {
        gridMesh = null;

        foreach (var c in this.GetComponentsInChildren<DebugGridFlag>())
        {
            GameObject.DestroyImmediate(c.transform.gameObject);
        }
        
        DestroyImmediate(transform.GetComponent<GridSpawner>());
    }


    private Vector3 MainPos2ViewPos(Vector3 pos)
    {
        float rad = -(float)Math.PI / 4.0f;           // 逆时针旋转45度
        Vector3 result = new Vector3();
        result.x = (float) (Math.Cos(rad) * pos.x + Math.Sin(rad) * pos.z);
        result.y = pos.y;
        result.z = (float) (-Math.Sin(rad) * pos.x + Math.Cos(rad) * pos.z);
        return result;
    }

    private Vector3 ViewPos2MainPos(Vector3 pos)
    {
        float rad = (float)Math.PI / 4.0f;           // 逆时针旋转45度
        Vector3 result = new Vector3();
        result.x = (float) (Math.Cos(rad) * pos.x + Math.Sin(rad) * pos.z);
        result.y = pos.y;
        result.z = (float) (-Math.Sin(rad) * pos.x + Math.Cos(rad) * pos.z);
        return result;
    }
    
}
